/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package All.Mapper;

import All.DTO.SanphamDTO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author NGUYEN DANG
 */
public class SanphamMapper extends MapperDB
{
    public SanphamMapper() throws Exception 
        {
		super();
	}

    public ArrayList<SanphamDTO> listSanpham(String id) throws Exception {
        ArrayList<SanphamDTO> listOfInfo = new ArrayList<SanphamDTO>();
        SanphamDTO sanphamDTO = null;
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT * FROM SanPham where Nhomsanpham='"+id+"'");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while ((rs != null) && rs.next()) {
                    sanphamDTO = new SanphamDTO();
                    initSanphamDTO(sanphamDTO, rs);
                    listOfInfo.add(sanphamDTO);
            }

            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return listOfInfo;
    }

    private void initSanphamDTO(SanphamDTO sanphamDTO, ResultSet rs) throws SQLException {
        if ((rs != null) && (sanphamDTO != null)) 
        {
            
            sanphamDTO.setMatintuc(rs.getString("Mabantin"));
            sanphamDTO.setTieude(rs.getString("Tieude"));
            sanphamDTO.setTomtat(rs.getString("Tomtat"));
            sanphamDTO.setNoidung(rs.getString("Noidung"));
            sanphamDTO.setHinhanh(rs.getString("Hinhanh"));
            sanphamDTO.setNgaydang(rs.getString("Ngaydang"));
            sanphamDTO.setNhomsanpham(rs.getString("Nhomsanpham"));
            sanphamDTO.setUserid(rs.getString("Nguoidang"));
        }
    }

    public SanphamDTO getNewsInformation(int matintuc) throws Exception {
        SanphamDTO sanphamDTO = new SanphamDTO();

        try {
                StringBuilder sql = new StringBuilder();
                sql.append("SELECT * FROM SanPham WHERE ");
                sql.append(" Mabantin = '" + matintuc + "'");

                PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
                ResultSet rs = stmt.executeQuery();
                if ((rs != null) && rs.next()) {
                        initTintucDTO(sanphamDTO, rs);
                }

                stmt.close();
        }
        catch (Exception e) {
                throw e;
        }

        return sanphamDTO;
    }

    private void initTintucDTO(SanphamDTO sanphamDTO, ResultSet rs) throws SQLException {
        if ((rs != null) && (sanphamDTO != null)) 
        {
            
            sanphamDTO.setMatintuc(rs.getString("Mabantin"));
            sanphamDTO.setTieude(rs.getString("Tieude"));
            sanphamDTO.setTomtat(rs.getString("Tomtat"));
            sanphamDTO.setNoidung(rs.getString("Noidung"));
            sanphamDTO.setHinhanh(rs.getString("Hinhanh"));
            sanphamDTO.setNgaydang(rs.getString("Ngaydang"));
            sanphamDTO.setNhomsanpham(rs.getString("Nhomsanpham"));
            sanphamDTO.setUserid(rs.getString("Nguoidang"));
        }
    }

    public boolean ADDeleteItem(String matin) throws Exception {
        boolean found = false;
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("Delete from SanPham where Mabantin=");
            sql.append("'"+matin+"'");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }

    public boolean ADUpdateItem(SanphamDTO sanphamDTO) throws Exception {
        boolean found = false;
        int tmp = Integer.parseInt(sanphamDTO.getMatintuc());
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE SanPham set Tieude='" + sanphamDTO.getTieude() + "',");
            sql.append(" Tomtat='" + sanphamDTO.getTomtat() + "',");
            sql.append(" Noidung='" + sanphamDTO.getNoidung() + "',");
            sql.append(" Hinhanh='" + sanphamDTO.getHinhanh() + "',");
            sql.append(" Ngaydang='" + sanphamDTO.getNgaydang() + "',");            
            sql.append(" Nguoidang='" + sanphamDTO.getUserid() + "'");
            sql.append(" where Mabantin = '"+ tmp + "'");          
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }

    public boolean InsertItem(SanphamDTO sanphamDTO) throws Exception {
        boolean found = false;
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("insert into SanPham(Tieude,Tomtat,Noidung,Hinhanh,Ngaydang,Nhomsanpham,Nguoidang) values(");
            sql.append("'" + sanphamDTO.getTieude() +"',");
            sql.append("'" + sanphamDTO.getTomtat() +"',");
            sql.append("'" + sanphamDTO.getNoidung() +"',");
            sql.append("'" + sanphamDTO.getHinhanh() +"',");
            sql.append("'" + sanphamDTO.getNgaydang() +"',");
            sql.append("'" + sanphamDTO.getNhomsanpham() +"',");
            sql.append("'" + sanphamDTO.getUserid() +"')");
            
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());

            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }
}
